package com.happle.gcmclient.utility;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.happle.gcmclient.R;
import com.happle.gcmclient.adapter.StatusDialogAdapter;

import org.w3c.dom.Text;

/**
 * DialogUtility
 * 
 * @author Lemon
 */
public final class DialogUtility {
	/**
	 * Show an alert dialog box
	 * 
	 * @param context
	 * @param message
	 */

	public static void alert(Context context, String message) {
		// if (context != null) {
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
		alertDialog.setTitle(context.getString(R.string.app_name));
		alertDialog.setMessage(message);
		alertDialog.setPositiveButton(R.string.btn_ok,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface arg0, int arg1) {
					}
				});
		alertDialog.show();
	}

	// }
	/**
	 * Show an alert dialog box
	 * 
	 * @param context
	 * @param messageId
	 */
	public static void alert(Context context, int messageId) {
		// if (context != null) {
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
		alertDialog.setTitle(context.getString(R.string.app_name));
		alertDialog.setMessage(context.getString(messageId));
		alertDialog.setPositiveButton(R.string.btn_ok,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface arg0, int arg1) {
					}
				});
		alertDialog.show();
		// }
	}

	public static void showOkDialog(Context context, int messageId,
			int OkTextId, final DialogInterface.OnClickListener onOKClick) {
		if (context != null) {
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
			alertDialog.setTitle(context.getString(R.string.app_name));
			alertDialog.setMessage(context.getString(messageId));

			alertDialog.setPositiveButton(OkTextId, onOKClick);
			alertDialog.show();
		}
	}

	public static void showYesNoDialog(final Context context, int messageId,
			int OkTextId, int cancelTextId,
			final DialogInterface.OnClickListener onOKClick) {
		if (context != null) {
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
			alertDialog.setTitle(context.getString(R.string.app_name));
			alertDialog.setMessage(context.getString(messageId));
			alertDialog.setNegativeButton(cancelTextId,
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.cancel();

						}
					});
			alertDialog.setPositiveButton(OkTextId, onOKClick);
			alertDialog.show();
		}
	}

	public static void showYesNoDialog(final Context context, int messageId,
			int OkTextId, int cancelTextId,
			final DialogInterface.OnClickListener onOKClick,
			final DialogInterface.OnClickListener onCancelClick) {
		if (context != null) {
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
			alertDialog.setTitle(context.getString(R.string.app_name));
			alertDialog.setMessage(context.getString(messageId));
			alertDialog.setNegativeButton(cancelTextId, onCancelClick);
			alertDialog.setPositiveButton(OkTextId, onOKClick);
			alertDialog.show();
		}
	}
	public static void showSimpleOptionDialog(Activity mContext, int titleId,
			String[] items, String positiverButton,
			DialogInterface.OnClickListener itemOnClick,
			DialogInterface.OnClickListener positiveOnClick) {
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setTitle(mContext.getString(titleId));
		builder.setItems(items, itemOnClick);
		builder.setPositiveButton(positiverButton, positiveOnClick);
		AlertDialog alert = builder.create();
		alert.show();
	}

    public static void showStatusDialog(Activity mContext, int titleId,
                                              String[] items, Integer[] images, String positiverButton,
                                              AdapterView.OnItemClickListener itemOnClick,
                                              DialogInterface.OnClickListener positiveOnClick, AlertDialog dialog) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        LayoutInflater inflater = mContext.getLayoutInflater();
        View view= inflater.inflate(R.layout.status_dialog_layout_listview, null);

        builder.setView(view);
        ListView lv = (ListView)view.findViewById(R.id.statusDialogListview);
        StatusDialogAdapter statusAdapter = new StatusDialogAdapter(mContext, images, items);
        lv.setAdapter(statusAdapter);
        lv.setOnItemClickListener(itemOnClick);

        TextView mainTitle = (TextView)view.findViewById(R.id.dialogMainTitle);
        mainTitle.setText(mContext.getResources().getString(titleId));
        builder.setCustomTitle(null);
        lv.setDivider(null);
        builder.setPositiveButton(positiverButton, positiveOnClick);

        //final AlertDialog dialog = builder.create();
        dialog = builder.create();
        // show it
        dialog.show();

    }

	public static void showSimpleOptionDialog2(Context mContext, int titleId,
			String[] items, String positiverButton, int selectIndex,
			DialogInterface.OnClickListener positiveOnClick) {
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setTitle(mContext.getString(titleId));
		builder.setSingleChoiceItems(items, selectIndex, null);
		builder.setPositiveButton(positiverButton, positiveOnClick);
		builder.setNegativeButton(mContext.getText(R.string.btn_cancel),
				new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();

					}
				});
		AlertDialog alert = builder.create();
		alert.show();
	}
    public static void showSimpleOptionDialogNeutralBtn(Activity mContext, int titleId,
                                              String[] items, String neutralBtn,
                                              DialogInterface.OnClickListener itemOnClick) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setTitle(mContext.getString(titleId));
        builder.setNeutralButton(neutralBtn,
                new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();

                    }
                });
        ArrayAdapter<String> dialogAdapter = new ArrayAdapter<String>(mContext,
                R.layout.simple_dialog_item, items);
        builder.setAdapter(dialogAdapter, itemOnClick);
        TextView textView = new TextView(mContext);
        textView.setTextColor(mContext.getResources().getColor(R.color.happle_button_color));
        textView.setText(titleId);
        textView.setTextSize(20);
        textView.setGravity(Gravity.CENTER);
        LinearLayout layout = new LinearLayout(mContext);
        layout.setWeightSum(1);
        layout.setMinimumHeight(100);
        layout.setGravity(Gravity.CENTER);
        layout.setBackgroundResource(R.color._darkblue);
        layout.addView(textView);
        builder.setCustomTitle(layout);
        AlertDialog alert = builder.create();
        alert.show();

        Button btnCancel = alert.getButton(DialogInterface.BUTTON_NEUTRAL);
        if (btnCancel != null) {
            btnCancel.setBackgroundResource(R.drawable.system_button_style);
            btnCancel.setTextSize(20);
            btnCancel.setTextColor(mContext.getResources().getColor(R.color._white));
            btnCancel.setMinHeight(80);
            btnCancel.setGravity(Gravity.CENTER);
        }
    }

	public static void alert(Context context, String message,
			DialogInterface.OnClickListener onOkClick) {
		if (context != null) {
			AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
			alertDialog.setTitle(R.string.app_name);
			alertDialog.setMessage(message);
			alertDialog.setPositiveButton(R.string.btn_ok, onOkClick);
			alertDialog.show();
		}
	}

}
